package com.sicent.operate.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FormatUtils {

	private static final SimpleDateFormat SDF_MINUTE = new SimpleDateFormat("yyyy-MM-dd HH:mm"), 
																		SDF_MINUTE1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), 
																		SDF_MINUTE2 = new SimpleDateFormat("yy-MM-dd HH:mm"), 
																		SDF_MONTH = new SimpleDateFormat("yyyy-MM"),
																		SDF_DAY = new SimpleDateFormat("yyyy-MM-dd");
	public static final int ONE_WEEK = 7 * 24 * 60 * 60, ONE_HOUR = 60 * 60,HALF_HOUR = 30 * 60, ONE_MINUTE = 60, ONE_DAY = 24 * 3600;

	/*
	 * 将字符串型的日期转为date型 time: 2015-12-07 00:00:00
	 */
	public static Date getDate(String time, String date_formate) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(date_formate);
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static int getSecond(String time) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(SDF_MINUTE.parse(time));
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static String getTimeStr(int second) {
		Date date = new Date((long) second * 1000);
		return SDF_MINUTE.format(date);
	}

	public static String getTimeStr1(int second) {
		Date date = new Date((long) second * 1000);
		return SDF_MINUTE1.format(date);
	}

	public static String getTimeStr2(int second) {
		Date date = new Date((long) second * 1000);
		return SDF_MINUTE2.format(date);
	}

	public static int getNextWeekStart(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int week = cal.get(Calendar.WEEK_OF_YEAR);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		if (month == 11 && week == 1) {
			year++;
		}
		if (day > 1)
			week++;
		day = 2;
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, week);
		cal.set(Calendar.DAY_OF_WEEK, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getThisWeekStart(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int week = cal.get(Calendar.WEEK_OF_YEAR);
		int day = cal.get(Calendar.DAY_OF_WEEK);
		if (month == 11 && week == 1) {
			year++;
		}
		if (day == 1)
			week--;
		day = 2;
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, week);
		cal.set(Calendar.DAY_OF_WEEK, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayBegin(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayEnd(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getMonthBegin(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getMonthEnd(int second) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) second * 1000);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayBegin(String day) throws ParseException {
		Date date = SDF_DAY.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayEnd(String day) throws ParseException {
		Date date = SDF_DAY.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 24);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayBegin(String day, DateFormat df)
			throws ParseException {
		Date date = df.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getDayEnd(String day, DateFormat df)
			throws ParseException {
		Date date = df.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static String getDayTimeString(int beginTime, int endTime) {
		return getHourMinute(beginTime) + "~" + getHourMinute(endTime);
	}

	public static String getHourMinute(int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) time * 1000);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		return hour + ":" + (minute < 10 ? ("0" + minute) : minute);
	}

	public static String getMonthDay(int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) time * 1000);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return month + "月" + day + "日";
	}

	public static int getWeekDay(int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) time * 1000);
		int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
		return day;
	}

	public static String getWeekDayString(int time) {
		int day = getWeekDay(time);
		switch (day) {
		case 0:
			return "周日";
		case 1:
			return "周一";
		case 2:
			return "周二";
		case 3:
			return "周三";
		case 4:
			return "周四";
		case 5:
			return "周五";
		case 6:
			return "周六";
		default:
			return null;
		}
	}

	public static String getWeekTimeString(int time) {
		int beginTime = getThisWeekStart(time);
		int endTime = getNextWeekStart(time) - 24 * 60 * 60;
		return getMonthDayString(beginTime) + "~" + getMonthDayString(endTime);
	}

	public static String getMonthDayString(int time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis((long) time * 1000);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return month + "月" + day + "日";
	}

	public static int getMonthBegin(String month) throws ParseException {
		Date date = SDF_MONTH.parse(month);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getMonthEnd(String month) throws ParseException {
		Date date = SDF_MONTH.parse(month);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int mon = cal.get(Calendar.MONTH) + 1;
		cal.set(Calendar.MONTH, mon);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	public static int getWeekBegin(String day) throws ParseException {
		Date date = SDF_DAY.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return getThisWeekStart((int) (cal.getTimeInMillis() / 1000));
	}

	public static int getWeekEnd(String day) throws ParseException {
		Date date = SDF_DAY.parse(day);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return getNextWeekStart((int) (cal.getTimeInMillis() / 1000));
	}

	public static boolean isInOneDay(int time1, int time2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTimeInMillis((long) time1 * 1000);
		cal2.setTimeInMillis((long) time2 * 1000);
		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2)
			return false;
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);
		if (day1 != day2)
			return false;
		return true;
	}

	public static int getDateSecond(Date date) throws ParseException {
		String dateStr = SDF_MINUTE.format(date);
		return getSecond(dateStr);
	}

	@SuppressWarnings("deprecation")
	public static int getCurrentMonth(Date date) {
		Integer month = date.getMonth();
		return month + 1;
	}

	public static String getWeekStr(int week) {
		switch (week) {
		case 0:
			return "周日";
		case 1:
			return "周一";
		case 2:
			return "周二";
		case 3:
			return "周三";
		case 4:
			return "周四";
		case 5:
			return "周五";
		case 6:
			return "周六";
		default:
			return null;
		}
	}

	public static String getSpecifiedDayBefore(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		String dayBefore = SDF_DAY.format(c.getTime());
		return dayBefore;
	}

	public static Date getDateBefore(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		return c.getTime();
	}

	/**
	 * 
	 * @param date
	 * @param afterType
	 *            1表示年 2 表示月 3 表示天
	 * @param afterSize
	 *            例如100年以后 2个月以后 或者 7天以后
	 * @return
	 */
	public static String getDateAfter(Date date, Integer afterType,
			Integer afterSize) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		switch (afterType) {
		case 1:
			int year = c.get(Calendar.YEAR);
			c.set(Calendar.YEAR, year + afterSize);
			break;
		case 2:
			int month = c.get(Calendar.MONTH);
			c.set(Calendar.MONTH, month + afterSize);
			break;
		case 3:
			int day = c.get(Calendar.DATE);
			c.set(Calendar.DATE, day + afterSize);
			break;
		default:
			break;
		}
		String dayAfter = SDF_MINUTE.format(c.getTime());
		return dayAfter;
	}
}
